// 员工管理功能实现

#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include "../util/InputFormat.h"
#include "StaffManage.h"

// 员工信息打印
// staffPointer: 员工链表指针
void StaffInfoPrint(ListPointer staffPointer)
{
    static int staffFlag = 0;
    char roleOfStaff[3][10] = {"服务员", "经理", "管理员"};
    struct Staff *staff;
    staff = (struct Staff *)(staffPointer->listData);
    if (staffFlag == 0)
    {
        system("cls");
        printf("\n\n\t\t*************** 员工信息管理界面 ***************\n\n\n");
        printf("\t\t     %-10s%-12s%-12s%-10s\n\n", "编号", "用户名", "角色", "状态");
        staffFlag = 1;
    }
    printf("\t\t     %-10d%-12s%-12s%-10s\n",
           staff->staffNo, staff->staffName, roleOfStaff[staff->StaffRole], staff->StaffState > 0 ? "在职" : "离职");
    if (staffPointer->listNext == NULL)
    {
        staffFlag = 0;
    }
}
// 添加员工
// data: 员工链表的最后一个节点数据指针
void *StaffAdd(void *data)
{
    char inputString[8] = {0};
    struct Staff *staffListData;
    struct Staff *tempStaffPointer;
    staffListData = (struct Staff *)malloc(sizeof(struct Staff));
    tempStaffPointer = (struct Staff *)data;
    staffListData->staffNo = (tempStaffPointer->staffNo) + 1;
    printf("\n\t\t==> 请输入要增加的用户名(10位内)：");
    FormatInputString(staffListData->staffName, 0, 0, 10, 1);
    while (1)
    {
        memset(staffListData->staffPassword, 0, sizeof(char));
        printf("\n\t\t==> 请输入要增加的密码(6位)：");
        FormatInputString(staffListData->staffPassword, '0', '9', 6, 2);
        printf("\n\t\t==> 再次确认输入密码(6位)：");
        FormatInputString(inputString, '0', '9', 6, 2);
        // 验证两次密码输入的一致性
        if (strcmp(staffListData->staffPassword, inputString) == 0)
        {
            break;
        }
        else
        {
            printf("\n\t\t!!! 密码不一致，请重新输入，按任意键继续...");
            memset(inputString, 0, sizeof(char));
            getch();
            printf("\n");
        }
    }
    printf("\n\t\t==> 请输入要增加的角色(0-服务员、1-经理、2-管理员)：");
    staffListData->StaffRole = FormatInputString(NULL, '0', '2', 1, 2);
    printf("\n\t\t==> 请输入要增加的状态(0-离职、1-在职):");
    staffListData->StaffState = FormatInputString(NULL, '0', '1', 1, 2);
    printf("\n");
    return staffListData;
}
// 员工删除，修改状态
// staffDataPointer: 员工链表的节点数据指针
int StaffRemove(ListPointer staffDataPointer)
{
    struct Staff *staff;
    static int staffState = 0;
    static int staffNumber = 0;
    static char inputString[8] = {0};
    if (staffState == 0) // 输入信息标志
    {
        printf("\n\n\t\t==> 请输入要删除的员工编号：");
        staffNumber = FormatInputString(inputString, '0', '9', 4, 2);
        staffState = 1;
        memset(inputString, 0, sizeof(char));
    }
    staff = (struct Staff *)(staffDataPointer->listData);
    if (staff->staffNo == staffNumber)
    {
        staff->StaffState = 0; // 员工状态改为离职，即为删除
        staffState = 0;
        return 1; // 删除成功
    }
    else
    {
        if (staffDataPointer->listNext == NULL)
        {
            staffState = 0;
        }
        return 0; // 删除失败
    }
}
// 员工信息更新
// staffDataPointer: 员工链表的节点数据指针
int StaffUpdate(ListPointer staffDataPointer)
{
    struct Staff *staff;
    static int staffState = 0;
    static int staffNumber = 0;
    static char inputString[8] = {0};
    if (staffState == 0)
    {
        printf("\n\n\t\t==> 请输入要修改的员工编号(4位)：");
        staffNumber = FormatInputString(inputString, '0', '9', 4, 2);
        staffState = 1;
        memset(inputString, 0, sizeof(char));
    }
    staff = (struct Staff *)(staffDataPointer->listData);
    if (staff->staffNo == staffNumber)
    {
        printf("\n\t\t==> 重新输入员工用户名：");
        FormatInputString(staff->staffName, 0, 0, 10, 1);
        while (1)
        {
            memset(staff->staffPassword, 0, sizeof(char));
            printf("\n\t\t==> 重新输入用户密码：");
            FormatInputString(staff->staffPassword, '0', '9', 6, 2);
            printf("\n\t\t==> 再次确认输入密码：");
            FormatInputString(inputString, '0', '9', 6, 2);
            if (strcmp(staff->staffPassword, inputString) == 0)
            {
                memset(inputString, 0, sizeof(char));
                break;
            }
            else
            {
                printf("\n\t\t!!! 密码不一致，请重新输入，按任意键继续...");
                memset(inputString, 0, sizeof(char));
                getch();
                printf("\n");
            }
        }
        printf("\n\t\t==> 重新输入员工角色(0-服务员、1-经理、2-管理员)：");
        staff->StaffRole = FormatInputString(NULL, '0', '2', 1, 2);
        printf("\n\t\t==> 重新输入员工状态(0-离职、1-在职)：");
        staff->StaffState = FormatInputString(NULL, '0', '1', 1, 2);
        staffState = 0;
        return 1; //   修改成功标志
    }
    else
    {
        if (staffDataPointer->listNext == NULL)
        {
            staffState = 0;
        }
        return 0; //   修改失败
    }
}
// 员工查询
// staffListHead: 员工链表的头指针
void StaffSearch(ListPointer staffListHead)
{
    int chooseItem;
    ListPointer staffDataPointer;
    ListPointer tempPointer;
    void (*pf[3])(int chooseItem, ListPointer tempPointer, ListPointer staffDataPointer) =
        {StaffSearchByState, StaffSearchByState, StaffSearchByNo};
    system("cls");
    staffDataPointer = LinkedListInit(NULL);
    printf("\n\n\t\t*************** 员工-查询条件选项 ***************\n\n");
    printf("\t\t\t\t    [1] 全部\n\n");
    printf("\t\t\t\t    [2] 离职\n\n");
    printf("\t\t\t\t    [3] 在职\n\n");
    printf("\t\t\t\t    [4] 编号");
    printf("\n\n\t\t==> 请输入要查询的条件:");
    chooseItem = FormatInputString(NULL, '1', '4', 1, 2);
    if (chooseItem == 1)
    {
        LinkedListPrint(staffListHead, StaffInfoPrint);
    }
    else
    {
        tempPointer = staffListHead->listNext;
        while (tempPointer != NULL)
        {
            pf[chooseItem - 2](chooseItem - 2, tempPointer, staffDataPointer);
            tempPointer = tempPointer->listNext;
        }
    }
}
// 按员工在职、离职状态查询
// state: 员工状态
// tempPointer: 员工节点指针
// staffDataPointer: 新申请的链表头指针
void StaffSearchByState(int state, ListPointer tempPointer, ListPointer staffDataPointer)
{
    struct Staff *staff;
    staff = (struct Staff *)(tempPointer->listData);
    if (staff->StaffState == state)
    {
        LinkedListAdd(staffDataPointer, staff); // 符合条件的结点添加进链表
    }
    if (tempPointer->listNext == NULL)
    {
        if (staffDataPointer->listNext != NULL)
        {
            LinkedListPrint(staffDataPointer, StaffInfoPrint);
            if (state == 1) // 判断状态
            {
                printf("\n\t\t==> 在职人数：%d人", LinkedListCount(staffDataPointer));
            }
            else
            {
                printf("\n\t\t==> 离职人数：%d人", LinkedListCount(staffDataPointer));
            }
        }
        else
        {
            printf("\n\n\t\t!!! 未查询到相关信息 !!!");
        }
    }
}
// 根据编号查询员工
// state: 员工状态
// tempPointer: 员工节点指针
// staffDataPointer: 新申请的链表头指针
void StaffSearchByNo(int state, ListPointer tempPointer, ListPointer staffDataPointer)
{
    static int staffNumber;
    static int staffState = 0;
    static char inputString[8] = {0};
    struct Staff *staff;
    staff = (struct Staff *)(tempPointer->listData);
    if (staffState == 0)
    {
        printf("\n\n\t\t==> 请输入要查询的编号(4位)：");
        staffNumber = FormatInputString(inputString, '0', '9', 4, 2);
        staffState = 1;
        memset(inputString, 0, sizeof(char));
    }
    if (staff->staffNo == staffNumber)
    {
        LinkedListAdd(staffDataPointer, staff);
    }
    if (tempPointer->listNext == NULL)
    {
        if (staffDataPointer->listNext != NULL)
        {
            LinkedListPrint(staffDataPointer, StaffInfoPrint);
        }
        else
        {
            printf("\n\n\t\t!!! 输入有误，未查询到相关信息 !!!");
        }
        staffState = 0;
    }
}
